package cn.hls.web.controller.dict;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.hls.entity.Person;
import cn.hls.entity.User;
import cn.hls.entity.vo.Msg;
import cn.hls.entity.vo.PageInfo;
import cn.hls.service.PersonService;
import cn.hls.service.UserService;
import cn.hls.until.GenericsUtils;
import cn.hls.web.controller.base.BaseController;

@Controller
@RequestMapping("/person")
public class PersonController extends BaseController<Person> {
	@Resource(name = "personService")
	private PersonService personService;
	@Resource(name = "userService")
	private UserService userService;

	@RequestMapping("/view")
	public String view() throws Exception {
		return "/dict/person";
	}

	@RequestMapping("/list")
	@ResponseBody
	public PageInfo<Person> list(PageInfo<Person> page, Person entity)
			throws Exception {
		return personService.loadPersonInfo(entity, page);
	}

	@RequestMapping("/add")
	@ResponseBody
	public Msg add(Person entity) throws Exception {
		boolean flag = false;
		/** 用户密码加密码 **/
		User user = entity.getUser();
		if (userService.login(user.getLoginName()) != null) {
			msg.setInfo("存在相同的登录账号!");
			msg.setStatus(201);
		} else {
			user.setSalt(GenericsUtils.getSalt());
			user.setPassword(GenericsUtils.encrypt(user.getPassword(),user.getSalt()));
			entity.setUser(user);
			flag = personService.add(entity);
			if (flag) {
				msg.setStatus(200);
				msg.setInfo("操作成功");
				msg.setSuccess(true);
			} else {
				msg.setStatus(201);
				msg.setInfo("操作失败");
				msg.setSuccess(false);
			}
		}
		return msg;
	}

	@RequestMapping("/update")
	@ResponseBody
	public Msg update(Person entity) throws Exception {
		boolean flag = true;
		flag = personService.update(entity);
		if (flag) {
			msg.setStatus(200);
			msg.setInfo("操作成功");
		} else {
			msg.setStatus(201);
			msg.setInfo("操作失败");
		}
		return msg;
	}

	@RequestMapping("/del")
	@ResponseBody
	public Msg del(String ids) throws Exception {
		String[] tempIds = ids.split("\\,");
		personService.deltedByIds(tempIds);
		msg.setInfo("删除成功");
		msg.setStatus(200);
		return msg;
	}
}
